<!--
 * @Author: your name
 * @Date: 2021-08-06 22:05:09
 * @LastEditTime: 2021-08-11 10:16:15
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \hrsaas-86\src\views\setting\index.vue
-->
<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              @click="clickShowAdd"
              size="small"
              type="primary"
            >
              新增角色
            </el-button>
            <!-- 表格 -->
            <el-table v-loading="loading" :data="tableData">
              <el-table-column
                type="index"
                :index="indexMethod"
                label="序号"
                width="120"
              />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <!-- row 行的信息  column列的信息  $index索引  stroe 表格内部数据管理 -->
                <template #default="{row}">
                  <!-- {{ fn(abc) }} -->
                  <!-- {{ row.id }} -->
                  <el-button
                    size="small"
                    @click="showEdit(row.id)"
                    type="primary"
                  >
                    编辑
                  </el-button>
                  <el-button size="small" type="success">修改</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="delRole(row.id)"
                  >
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              :current-page="page"
              :page-sizes="[1, 2, 3, 4]"
              :page-size="pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>

          <el-dialog title="弹层标题" :visible="showDialog" @close="btnCancel">
            <el-form
              ref="roleForm"
              :model="form"
              :rules="rules"
              label-width="100px"
            >
              <el-form-item label="角色名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入角色名称" />
              </el-form-item>
              <el-form-item label="角色描述" prop="description">
                <el-input
                  v-model="form.description"
                  placeholder="请输入角色描述"
                />
              </el-form-item>
            </el-form>

            <template #footer>
              <el-button @click="btnCancel">取消</el-button>
              <el-button type="primary" @click="submitBtn">确认</el-button>
            </template>
          </el-dialog>
          <el-tab-pane label="公司信息">
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="companyForm.name"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="companyForm.companyAddress"
                  disabled
                  style="width:400px"
                />
              </el-form-item>

              <el-form-item label="公司电话">
                <el-input
                  v-model="companyForm.companyPhone"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="companyForm.mailbox"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyForm.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
import { reqGetCompanyById } from '@/api/company'
import {
  reqGetRoleList,
  reqDeleteRole,
  reqAddRole,
  reqGetRoleDetail,
  reqUpdateRole
} from '@/api/setting'
// import { Loading } from 'element-ui'
export default {
  name: 'Setting',
  data() {
    return {
      activeName: 'roles',
      tableData: [], // 表格数据
      page: 1,

      pagesize: 2,
      total: 0, // 总条数
      Loading: false,
      showDialog: false,
      companyForm: '',
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入角色名称',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  created() {
    this.getRolesList() // 获取角色列表
    this.getCompanyInfo() // 获取公司信息
  },
  computed: {
    ...mapState('user', ['userInfo']),
    showTitle() {
      return this.form.id ? '编辑角色' : '添加角色'
    }
  },
  methods: {
    // 每页条数发生修改的时候会触发, 参数中是最新的每页条数
    handleSizeChange(newVal) {
      // console.log(newVal)
      // 将最新的pagesize同步 重新发送请求
      this.pagesize = newVal
      this.page = 1 // 只要pagesize发生了变化 从第一页开始展示数据
      this.getRolesList()
    },
    // 页码发生变化的时候会触发 参数是最新的页码 点击左右箭头页码 和页码跳转会触发这个函数
    handleCurrentChange(newVal) {
      // console.log(newVal)
      // 将得到的最新的value 携带重新发送请求
      this.page = newVal
      this.getRolesList()
    },
    indexMethod(index) {
      return index + 1 + (this.page - 1) * this.pagesize
    },
    async getRolesList() {
      this.loading = true
      const {
        data: { rows, total }
      } = await reqGetRoleList(this.page, this.pagesize)
      this.tableData = rows
      this.total = total
      this.loading = false
    },
    // 删除角色
    delRole(id) {
      // console.log(id)
      // 1. 确认消息框
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(async () => {
          // 2. 发送请求
          const { message } = await reqDeleteRole(id)
          //  3. 提示用户
          this.$message.success(message)
          // bug处理 只剩一条数据 删除完毕后 page--
          if (this.tableData.length === 1 && this.page > 1) {
            this.page--
          }
          // 4. 重新渲染
          this.getRolesList()
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },
    clickShowAdd() {
      this.showDialog = true
    },
    btnCancel() {
      this.showDialog = false
      this.form = {
        name: '',
        description: ''
      }
      // 重置表单数据
      this.$refs.roleForm.resetFields()
    },
    async showEdit(id) {
      const { data } = await reqGetRoleDetail(id)
      this.form = data
      this.showDialog = true
    },
    submitBtn() {
      this.$refs.roleForm.validate(async flag => {
        if (!flag) return
        if (this.form.id) {
          await reqUpdateRole(this.form)
        } else {
          await reqAddRole(this.form)
        }
        // 关闭弹框
        this.showDialog = false
        // 重新渲染
        this.getRolesList()
        // 添加成功
        this.$message.success('添加成功')
      })
    },
    async getCompanyInfo() {
      const { data } = await reqGetCompanyById(this.userInfo.companyId)
      console.log(this.userInfo)
      this.companyForm = data
    }
  }
}
</script>
